
global path "YOUR_PATH\Replication_Code"

use "$path\01_InData\monthly_crsp.dta", clear
rename RET ret
rename DLRET dlret
rename DLSTCD dlstcd
replace dlret=-.3 if (dlstcd== 500 | (dlstcd>=520 & dlstcd<=584)) & missing(dlret)

egen tot_ret=rowtotal(ret dlret)
gen monthdate=mofd(date)
sort PERMNO monthdate
tsset PERMNO monthdate
gen f1ret=f.tot_ret
gen prvmret=tot_ret
gen mom=l.ret
forval k=2/5{
    replace mom=(1+mom)*(1+l`k'.tot_ret)-1
}
forval k=2/24{
    local j=`k'-1
    gen f`k'ret=(1+f`j'ret)*(1+f`k'.ret)-1
}
gen lpermno=PERMNO
save "$path\01_InData\monthly_crsp.dta", replace

use "$path\01_InData\fundamentals_mb.dta", clear
gen mktvalue=cshoq*prccq
gen bm=ceqq/mktvalue
keep if bm>0 & bm<=100
gen btm=log(bm)
gen size=log(mktvalue)
gen monthdate=mofd(datadate)
rename LPERMNO lpermno
sort lpermno monthdate
by lpermno monthdate: gen dup=cond(_N==1, 0, _n)
keep if dup<1
save "$path\01_InData\fundamentals_mb.dta", replace

use "$path\01_InData\monthly_crsp.dta", clear

merge 1:1 lpermno monthdate using "$path\01_InData\fundamentals_mb.dta"
keep if _merge==1 | _merge==3
sort lpermno monthdate
by lpermno monthdate: gen dup1=cond(_N==1,0,_n)
keep if dup1<1

sort lpermno monthdate
xtset lpermno monthdate

gen btmlag=btm[_n-1]
gen btmlag2=btm[_n-2] 
gen btmlag3=btm[_n-3] 

replace btm=l.btm if missing(btm) & ~missing(btmlag)
replace btm=l2.btm if missing(btm) & ~missing(btmlag2)
replace btm=l3.btm if missing(btm) & ~missing(btmlag3)


gen sizelag=size[_n-1]
gen sizelag2=size[_n-2] 
gen sizelag3=size[_n-3] 


replace size=l.size if missing(size) & ~missing(sizelag)
replace size=l2.size if missing(size) & ~missing(sizelag2)
replace size=l3.size if missing(size) & ~missing(sizelag3)


gen mktvaluelag=mktvalue[_n-1]
gen mktvaluelag2=mktvalue[_n-2] 
gen mktvaluelag3=mktvalue[_n-3] 


replace mktvalue=l.mktvalue if missing(mktvalue) & ~missing(mktvaluelag)
replace mktvalue=l2.mktvalue if missing(mktvalue) & ~missing(mktvaluelag2)
replace mktvalue=l3.mktvalue if missing(mktvalue) & ~missing(mktvaluelag3)

drop _merge
save "$path\02_OutData\ret_fund_mb.dta", replace

use "$path\02_OutData\forecasts_act_ltg_ind.dta", clear
drop if revision<-200 | revision>200
gen buy_pos=(good==1 & revision>=5 & ~missing(revision))
gen buy_neg=(good==1 & revision<=-5 & ~missing(revision))

gen sell_pos=(bad==1 & revision>=5 & ~missing(revision))
gen sell_neg=(bad==1 & revision<=-5 & ~missing(revision))

gen monthdate=mofd(anndats_revision)


bys PERMNO monthdate: egen tot_buy_pos=total(buy_pos)
bys PERMNO monthdate: egen tot_buy_neg=total(buy_neg)
bys PERMNO monthdate: egen tot_sell_pos=total(sell_pos)
bys PERMNO monthdate: egen tot_sell_neg=total(sell_neg)
bys PERMNO monthdate: egen mean_revision=mean(revision)

bys PERMNO monthdate: gen dup=cond(_N==1,0,_n)
keep if dup<2
keep PERMNO monthdate tot_buy_pos tot_buy_neg tot_sell_pos tot_sell_neg mean_revision

save "$path\02_OutData\monthly_ibes.dta", replace

use "$path\02_OutData\ret_fund_mb.dta", clear
merge 1:1 PERMNO monthdate using "$path\02_OutData\monthly_ibes.dta"

drop if _merge==2

gen buy_neg=1 if tot_buy_neg>0 & ~missing(tot_buy_neg)
replace buy_neg=0 if missing(buy_neg)


gen buy_pos=1 if tot_buy_pos>0 & ~missing(tot_buy_pos)
replace buy_pos=0 if missing(buy_pos)


gen sell_neg=1 if tot_sell_neg>0 & ~missing(tot_sell_neg)
replace sell_neg=0 if missing(sell_neg)


gen sell_pos=1 if tot_sell_pos>0 & ~missing(tot_sell_pos)
replace sell_pos=0 if missing(sell_pos)

gen mr1=mean_revision
replace mr1=0 if missing(mean_revision)

gen dum=0 
replace dum=1 if missing(mean_revision) 

*Regressions for the table
gen year=year(date)
drop if year<1993

reghdfe f1ret buy_neg sell_neg buy_pos sell_pos mr1 dum, absorb(monthdate) vce(cluster PERMNO monthdate)

test buy_neg=sell_neg
test buy_pos=sell_pos

reghdfe f1ret prvmret mom btm size buy_neg sell_neg buy_pos sell_pos mr1 dum, absorb(monthdate) vce(cluster PERMNO monthdate)

test buy_neg=sell_neg
test buy_pos=sell_pos

reghdfe f6ret buy_neg sell_neg buy_pos sell_pos mr1 dum, absorb(monthdate) vce(cluster PERMNO monthdate)

test buy_neg=sell_neg
test buy_pos=sell_pos

reghdfe f6ret prvmret mom btm size buy_neg sell_neg buy_pos sell_pos mr1 dum, absorb(monthdate) vce(cluster PERMNO monthdate)

test buy_neg=sell_neg
test buy_pos=sell_pos

reghdfe f36ret buy_neg sell_neg buy_pos sell_pos mr1 dum, absorb(monthdate) vce(cluster PERMNO monthdate)

test buy_neg=sell_neg
test buy_pos=sell_pos

reghdfe f36ret prvmret mom btm size buy_neg sell_neg buy_pos sell_pos mr1 dum, absorb(monthdate) vce(cluster PERMNO monthdate)

test buy_neg=sell_neg
test buy_pos=sell_pos

